#ifndef LASERDEVICE_H
#define LASERDEVICE_H

#include <QSettings>
#include "jsonconvert.h"

enum LaserMode{
    CO2 = 0,
    FIBER = 1,
    YAG = 2,
    UV = 3,
    QCW = 30,
};

class LaserDevice : public JsonConvert
{
public:
    LaserDevice(){}

    LaserMode laserMode()
    {
        return mLaserMode;
    }

    void setLaserMode(LaserMode m)
    {
        mLaserMode = m;
    }

    int power()
    {
        return mPower;
    }

    void setPower(int p)
    {
        mPower = p;
    }

    double frequency()
    {
        return mFrequency;
    }

    void setFrequency(double frequency)
    {
        mFrequency = frequency;
    }

    bool frequencyLocker()
    {
        return mFrequencyLocker;
    }

    void setFrequencyLocker(bool frequencyLocker)
    {
        mFrequencyLocker = frequencyLocker;
    }

    int speed()
    {
        return mSpeed;
    }

    void setSpeed(int speed)
    {
        mSpeed = speed;
    }

    bool speedLocker()
    {
        return mSpeedLocker;
    }

    void setSpeedLocker(bool speedLocker)
    {
        mSpeedLocker = speedLocker;
    }

    double spotDistance()
    {
        return mSpotDistance;
    }

    void setSpotDistance(double spotDistance)
    {
        mSpotDistance = spotDistance;
    }

    bool spotDistanceLocker()
    {
        return mSpotDistanceLocker;
    }

    void setSpotDistanceLocker(bool spotDistanceLocker)
    {
        mSpotDistanceLocker = spotDistanceLocker;
    }

    double pulseWidth()
    {
        return mPulseWidth;
    }

    void setPulseWidth(double pulseWidth)
    {
        mPulseWidth = pulseWidth;
    }

    int defocus()
    {
        return mDefocus;
    }

    void setDefocus(int defocus)
    {
        mDefocus = defocus;
    }

    double lineWidth()
    {
        return mLineWidth;
    }

    void setLineWidth(double lw)
    {
        mLineWidth = lw;
    }

    bool cw()
    {
        return mCW;
    }

    void setCW(bool cw)
    {
        mCW = cw;
    }


    void readFromFile()
    {
        QSettings settings("GlobalLaser.ini", QSettings::IniFormat);
        settings.beginGroup("Laser");
        this->mLaserMode = static_cast<LaserMode>(settings.value("mLaserMode", 0).toInt());
        this->mPower = settings.value("mPower", 20).toInt();
        this->mFrequency = settings.value("mFrequency", 50).toDouble();
        this->mFrequencyLocker = settings.value("mFrequencyLocker").toBool();
        this->mSpeed = settings.value("mSpeed", 1000).toInt();
        this->mSpeedLocker = settings.value("mSpeedLocker").toBool();
        this->mSpotDistance = settings.value("mSpotDistance", 0.02).toDouble();
        this->mSpotDistanceLocker = settings.value("mSpotDistanceLocker").toBool();
        this->mPulseWidth = settings.value("mPulseWidth", 10.0).toDouble();
        this->mDefocus = settings.value("mDefocus", 0).toInt();
        this->mLineWidth = settings.value("mLineWidth", 0.1).toInt();
        this->mCW = settings.value("mCW").toBool();
        settings.endGroup();
    }

    void writeToFile()
    {
        QSettings settings("GlobalLaser.ini", QSettings::IniFormat);
        settings.beginGroup("Laser");
        settings.setValue("mLaserMode", mLaserMode);
        settings.setValue("mPower", mPower);
        settings.setValue("mFrequency", mFrequency);
        settings.setValue("mFrequencyLocker", mFrequencyLocker);
        settings.setValue("mSpeed", mSpeed);
        settings.setValue("mSpeedLocker", mSpeedLocker);
        settings.setValue("mSpotDistance", mSpotDistance);
        settings.setValue("mSpotDistanceLocker", mSpotDistanceLocker);
        settings.setValue("mPulseWidth", mPulseWidth);
        settings.setValue("mDefocus", mDefocus);
        settings.setValue("mLineWidth", mLineWidth);
        settings.setValue("mCW", mCW);
        settings.endGroup();
    }

    QJsonObject toJsonObj() const
    {
        QJsonObject obj;
        obj["mLaserMode"] = mLaserMode;
        obj["mPower"] = mPower;
        obj["mFrequency"] = mFrequency;
        obj["mFrequencyLocker"] = mFrequencyLocker;
        obj["mSpeed"] = mSpeed;
        obj["mSpeedLocker"] = mSpeedLocker;
        obj["mSpotDistance"] = mSpotDistance;
        obj["mSpotDistanceLocker"] = mSpotDistanceLocker;
        obj["mPulseWidth"] = mPulseWidth;
        obj["mDefocus"] = mDefocus;
        obj["mLineWidth"] = mLineWidth;
        obj["mCW"] = mCW;
        return obj;
    }

    void fromJsonObj(const QJsonObject& obj)
    {
        mLaserMode = static_cast<LaserMode>(obj["mLaserMode"].toInt());
        mPower = obj["mPower"].toInt();
        mFrequency = obj["mFrequency"].toDouble();
        mFrequencyLocker = obj["mFrequencyLocker"].toBool();
        mSpeed = obj["mSpeed"].toInt();
        mSpeedLocker = obj["mSpeedLocker"].toBool();
        mSpotDistance = obj["mSpotDistance"].toDouble();
        mSpotDistanceLocker = obj["mSpotDistanceLocker"].toBool();
        mPulseWidth = obj["mPulseWidth"].toDouble();
        mDefocus = obj["mDefocus"].toInt();
        mLineWidth = obj["mLineWidth"].toDouble();
        mCW = obj["mCW"].toBool();
    }

    bool operator ==(const LaserDevice& param){
        bool rel = true;
        rel &= (this->mLaserMode == param.mLaserMode);
        rel &= (this->mPower == param.mPower);
        rel &= (this->mFrequency == param.mFrequency);
        rel &= (this->mFrequencyLocker == param.mFrequencyLocker);
        rel &= (this->mSpeed == param.mSpeed);
        rel &= (this->mSpeedLocker == param.mSpeedLocker);
        rel &= (this->mSpotDistance == param.mSpotDistance);
        rel &= (this->mSpotDistanceLocker == param.mSpotDistanceLocker);
        rel &= (this->mPulseWidth == param.mPulseWidth);
        rel &= (this->mDefocus == param.mDefocus);
        rel &= (this->mLineWidth == param.mLineWidth);
        rel &= (this->mCW == param.mCW);
        return rel;
    }

    void operator =(const LaserDevice& param){
        this->mLaserMode = param.mLaserMode;
        this->mPower = param.mPower;
        this->mFrequency = param.mFrequency;
        this->mFrequencyLocker = param.mFrequencyLocker;
        this->mSpeed = param.mSpeed;
        this->mSpeedLocker = param.mSpeedLocker;
        this->mSpotDistance = param.mSpotDistance;
        this->mSpotDistanceLocker = param.mSpotDistanceLocker;
        this->mPulseWidth = param.mPulseWidth;
        this->mDefocus = param.mDefocus;
        this->mLineWidth = param.mLineWidth;
        this->mCW = param.mCW;
    }

private:
    LaserMode mLaserMode = CO2;
    int mPower = 0;
    double mFrequency = 0;//kHz
    bool mFrequencyLocker = false;
    int mSpeed = 0;
    bool mSpeedLocker = false;
    double mSpotDistance = 0;
    bool mSpotDistanceLocker = false;
    double mPulseWidth = 0.0;
    int mDefocus = 0;
    double mLineWidth = 0.1;
    bool mCW = false;
};

#endif // LASERDEVICE_H
